package com.lagou.work.task03.angel.utils;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.List;

/**
 * @author：dingyuan
 * @date：2022.4.1 21:15
 * @description：IO连接关闭工具类
 * @version: 1.0
 */
public class IOUtil {


    /**
     *获取磁盘路径文件的内容
     */
    public static String readFileFromLocal(String path){

        FileReader fReader = null;
        BufferedReader buffReader = null;
        StringBuffer strBuffer = new StringBuffer();
        try {
            fReader = new FileReader(new File(path));
            buffReader = new BufferedReader(fReader);
            char[] buffer = new char[1024];
            int pos;
            while ((pos = buffReader.read(buffer)) != -1) {
                strBuffer.append(String.valueOf(buffer));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != fReader){
                    fReader.close();
                }
                if(null != buffReader){
                    buffReader.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return strBuffer.toString().trim();
    }

    /**
     *输出内容到磁盘路径文件
     */
    public static void writeFile2Local(String outPutPath, String outCharArr){

        FileWriter fileWriter = null;
        BufferedWriter buffWriter = null;
        File file = null;
        try {
            file = new File(outPutPath);
            createFile(file);
            fileWriter = new FileWriter(file);
            buffWriter = new BufferedWriter(fileWriter);
            buffWriter.write(outCharArr);
            buffWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(null != fileWriter){
                    fileWriter.close();
                }
                if(null != buffWriter){
                    buffWriter.close();
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     *创建目标文件及目录，当目标文件目录不存在时
     */
    public static synchronized void createFile(File file){
        if(file.exists()){
            System.out.println("文件已存在");
        }else {
            if(!file.getParentFile().exists()){
                // 目标路径的父目录不存在则创建目录
                file.getParentFile().mkdirs();
            }
            try {
                // 创建完目录最后需要创建目标文件
                file.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
        }
    }
    /**
     *处理字节流
     */
    public static OutputStream handleIOStream(InputStream in, OutputStream os ) throws IOException {

        OutputStream bos = null;
        if(os.getClass().isAssignableFrom(ByteArrayOutputStream.class)){
            bos = os;
        }else {
            bos = new BufferedOutputStream(os);
        }
        try {
            byte[] buffer = new byte[1024];
            int pos;
            while ((pos = in.read(buffer)) != -1) {
                bos.write(buffer, 0, pos);
            }
            bos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(! os.getClass().isAssignableFrom(ByteArrayOutputStream.class)){
                if(null != bos){
                    bos.close();
                }
            }
            IOUtil.close(null, in, os);
        }
        return bos;
    }
    /**
     * 关闭连接1
     */
    public static void close( Socket socket, InputStream inputStream, ByteArrayOutputStream byteArrOutStream , PrintStream printStream){
        try {
            if(null != socket){
                socket.close();
            }
            if(null != printStream){
                printStream.close();
            }
            if(null != inputStream){
                inputStream.close();
            }

            if(null != byteArrOutStream){
                byteArrOutStream.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接2
     */
    public static void close(Socket socket, FileInputStream fis, BufferedOutputStream bos, OutputStream os, ByteArrayOutputStream baos) {
        try {
            if (null != socket) {
                socket.close();
            }
            if (null != fis) {
                fis.close();
            }
            if (null != bos) {
                bos.close();
            }
            if (null != os) {
                os.close();
            }
            if (null != baos) {
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接3
     */
    public static void close( Socket socket, ServerSocket serverSocket ){
        try {
            if(null != socket){
                socket.close();
            }
            if(null != serverSocket){
                serverSocket.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接4
     */
    public static void close(Socket socket, InputStream in, OutputStream os){
        try {
            if(null != socket){
                socket.close();
            }
            if(null != in){
                in.close();
            }
            if(null != os){
                os.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接5
     */
    public static void close(List<Object> clientSockets,List<Object> serverSockets, FileInputStream fis, BufferedOutputStream bos, OutputStream os, InputStream in, ByteArrayOutputStream baos) {
        try {
            if (null != clientSockets) {
                for (Object clientSocket : clientSockets) {
                    ((Socket)clientSocket).close();
                }
            }
            if (null != serverSockets) {
                for (Object serverSocket : serverSockets) {
                    ((ServerSocket)serverSocket).close();
                }
            }
            if (null != fis) {
                fis.close();
            }
            if (null != bos) {
                bos.close();
            }
            if (null != os) {
                os.close();
            }
            if (null != in) {
                in.close();
            }
            if (null != baos) {
                baos.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
